package com.starsea.book.ui.newpage

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.starsea.book.R
import com.starsea.book.base.VMBaseFragment
import com.starsea.book.databinding.FragmentWalletCoinBinding
import com.starsea.book.ui.newpage.activity.CoinExchangeActivity
import com.starsea.book.ui.newpage.adapter.WalletDetailsAdapter
import com.starsea.book.ui.newpage.utils.finialDouble
import com.starsea.book.ui.newpage.viewmodel.WalletViewModel
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-02-23 10 GMT+8
 * @desc :
 */
class WalletCoinFragment : VMBaseFragment<WalletViewModel>(R.layout.fragment_wallet_coin) {

    private val binding by viewBinding(FragmentWalletCoinBinding::bind)
    override val viewModel by viewModels<WalletViewModel>()

    lateinit var mAdapter: WalletDetailsAdapter

    var page = 1

    companion object {
        fun newInstance(): WalletCoinFragment {
            val args = Bundle()

            val fragment = WalletCoinFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {

        mAdapter = WalletDetailsAdapter(requireContext(), 1) {

        }

        binding.rvContent.layoutManager = LinearLayoutManager(requireContext())
        binding.rvContent.adapter = mAdapter

        binding.rtvExchange.setOnClickListener {
            startActivity<CoinExchangeActivity> { }
        }

        viewModel.walletInfoResult.observe(this) { data ->
            if (data.status == 200) {

                binding.tvNum.text =
                    if (TextUtils.isEmpty(data.data!!.goldBalance)) "0" else {
                        val goldBalance = data.data!!.goldBalance!!.toDouble()
                        if (goldBalance > 10000) {
                            "${(goldBalance / 10000.0).finialDouble()}万"
                        } else {
                            goldBalance.finialDouble()
                        }
                    }

                binding.tvTodayCoinNum.text =
                    (data.data!!.todayGold ?: "0").toDouble().finialDouble()
                binding.tvTotalNum.text =
                    (data.data!!.accumulatedGold ?: "0").toDouble().finialDouble()

                binding.tvBili.text = "${data.data!!.subRatio ?: ""}金币=1天"

                binding.rtvRmb.text = "约${
                    if (TextUtils.isEmpty(data.data!!.goldBalance)) "0" else {
                        val goldBalance = data.data!!.goldBalance!!.toDouble()
                        val ratio = data.data!!.subRatio!!.toDouble()
                        (goldBalance / ratio).finialDouble()
                    }
                }元"


            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(requireContext())
            } else {
                toastOnUi(data.msg)
            }
        }

        binding.srlRefresh.setOnRefreshListener {
            page = 1
            viewModel.getWalletDetails(1, page)
        }
        binding.srlRefresh.setOnLoadMoreListener {
            page++
            viewModel.getWalletDetails(1, page)
        }


        viewModel.getWalletDetails(1, page)

        viewModel.walletDetailsResult.observe(this) { data ->
            if (data.status == 200) {

                if (page == 1) {
                    binding.srlRefresh.finishRefresh()
                    mAdapter.setItems(data.data!!.list)
                } else {
                    binding.srlRefresh.finishLoadMore()
                    mAdapter.addItems(data.data!!.list!!)
                }

                if (data.data!!.list?.size!! < 10) {
                    binding.srlRefresh.finishLoadMoreWithNoMoreData()
                }

            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(requireContext())
            } else {
                toastOnUi(data.msg)
            }
        }

    }

    override fun onResume() {
        super.onResume()
        viewModel.getWalletInfo()
    }


}